//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 5
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace GetMimeTextViaProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Create a message to get MIME text for
            //
            ItemIdType item = EWSHelpers.CreateMessage(
                binding,
                "Here is a subject");

            Console.WriteLine(GetMimeTextViaProxy(binding, item));
        }

        /// <summary>
        /// Retrieves the MIME string from the item (Listing 5-40)
        /// </summary>
        /// <param name="binding">Binding to use for calls</param>
        /// <param name="itemId">Id of the item to retrieve MIME text for</param>
        /// <returns>Mime string</returns>
        /// 
        public static string GetMimeTextViaProxy(
                                      ExchangeServiceBinding binding,
                                      ItemIdType itemId)
        {
            GetItemType request = new GetItemType();

            // Let's use some of our handy partial class extenstions to build up our 
            // shape so that this example is a bit shorter :)  Here we just want the 
            // Id along with the mime content so the mime content will be specified 
            // as an additional property.
            //
            PathToUnindexedFieldType mimeContent = new
                    PathToUnindexedFieldType(UnindexedFieldURIType.itemMimeContent);
            request.ItemShape = new
                    ItemResponseShapeType(DefaultShapeNamesType.IdOnly, mimeContent);

            request.ItemIds = new BaseItemIdType[] { itemId };

            GetItemResponseType response = binding.GetItem(request);
            ItemInfoResponseMessageType responseMessage =
                   response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                ItemType item = responseMessage.Items.Items[0];
                if (item.MimeContent != null)
                {
                    return EWSHelpers.GetMimeText(item.MimeContent.Value);
                }
                else
                {
                    return String.Empty;
                }
            }
            else
            {
                throw new InvalidOperationException(
                     "Failed to get Mime Content.  Error " +
                           responseMessage.ResponseCode);
            }
        }

    }
}
